package com.vertx.core;

import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.file.FileSystem;
import io.vertx.core.http.HttpServer;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeoutException;

/**
 * @version v1.0
 * @Description: Vertx 异步任务协调
 * @Author: TongRui乀
 * @Date: 2021/5/5 3:54 下午
 */
public class VertxFuture {

    public static Future<String> future_01(){

        Future<String> httpServerFuture = Future.future(h->{
            try {
                Thread.sleep(1000);
                System.out.println("run task 1");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            h.complete("task1 sleep complete");
        });

        return httpServerFuture;
    }

    public static Future<String> future_02(){

        Future<String> httpServerFuture = Future.future(h->{
            try {
                Thread.sleep(3000);
                System.out.println("run task 2");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            h.complete("task 2 sleep complete");
        });

        return httpServerFuture;
    }

    public static Future<String> future_exception(){

        Future<String> httpServerFuture = Future.future(h->{
            try {
                Thread.sleep(2000);
                h.fail(new TimeoutException("timeout"));
                System.out.println("run task 3");
            } catch (Exception e) {
                h.fail(e);
            }
        });

        return httpServerFuture;
    }

    public static void futureCompose(){

        Future<String> future2 = Future.future(p->{
            p.complete("2");
        });

        Future<String> future = Future.future(o->{
            o.complete("1");
        })
        .compose(s-> {
            int a = 1/0;
            System.err.println(s);
            return future2;
        });


        future.onComplete(h->{
            System.out.println(h.result());
        });

    }

    public static void all(){

        var future = CompositeFuture.all(future_01(), future_02(), future_exception())
                .onComplete(h->{
                    if (h.succeeded()) {
                        h.result().list().stream().forEach(System.out::println);
                    }
                    if(h.failed()){
                        System.err.println(h.cause());
                    }
                });



    }

    public static void any(){
        CompositeFuture.any(future_01(), future_02(), future_exception())
                .onComplete(h->{
                    System.out.println("complete");
                    h.result().list().forEach(System.out::println);
                }).onSuccess(h->{
                    System.out.println("success");
                           h.list().forEach(System.out::println);
                        });
    }

    public static void join(){

        CompositeFuture.join(future_01(), future_02(), future_exception())
                .onComplete(h->{
                    h.result().list()
                            .forEach(System.out::println);
                });
    }

    public static void toCompleteStage(){

        future_01().toCompletionStage().whenComplete((s, e)->{
            System.out.println(s);
        });
    }

    public static void fromCompleteStage(){

        var future = Future.fromCompletionStage(CompletableFuture.supplyAsync(()->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "success";
        }))
        .onSuccess(System.out::println);
    }

    public static void main(String[] args) {

        fromCompleteStage();

        while (true);


    }

}
